package com.cn.tianxin.ssserver.controller;

import com.cn.tianxin.ssserver.service.WeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("/image")
public class ImageController {

    @Autowired
    WeixinService weixinService;

    @RequestMapping(value = "/authQRCode")
    @ResponseBody
    public void authQRCode(Long rand, HttpServletResponse response){

        System.out.println("rand=>"+rand);

        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            BufferedImage bufferedImage = weixinService.getAuthImage("tianxin");
            ImageIO.write(bufferedImage, "png", os);

            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
